package cn.itcast.ssm.controller;

import cn.itcast.ssm.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Created by t410 on 2017/6/29.
 */
@Controller
@RequestMapping("/UrlMapping")
public class UrlMapping {


    private String showJsp="show";

    @RequestMapping(value={"/puTong1","puTong2"})
    public String puTong1(Map map){
        System.out.println("多个URL路径可以映射到同一个处理器的功能处理方法。");
        map.put("v1","普通url映射");
        return showJsp;
    }

    /** URI模板模式映射_1
     *  @RequestMapping(value="/users/{userId}")：{×××}占位符， 请求的URL可以是 “/users/123456”或“/users/abcd”，通过@PathVariable可以提取URI模板模式中的{×××}中的×××变量。
     * http://127.0.0.1:8080/UrlMapping/template/user1.action
     */
    @RequestMapping(value="/template/{userId}")
    public String templateMapping1(@PathVariable("userId") String userId,Map map){
        System.out.println(userId);
        map.put("v1",userId);
        return showJsp;
    }

    /** URI模板模式映射_2
     *  RequestMapping(value="/users/{userId}/create")：这样也是可以的，请求的URL可以是“/users/123/create”。
     *
     */
    @RequestMapping(value="/template/{userId}/create")
    public String templateMapping2(@PathVariable("userId") String userId,Map map){
        System.out.println(userId);
        map.put("v1",userId);
        return showJsp;
    }

    /** URI模板模式映射_3
     *  RequestMapping(value="/template/{userId}/create")：这样也是可以的，请求的URL可以是“/users/123/create”。
     *
     */
    @RequestMapping(value="/template/{userId}/topics/{topicId}")
    public String templateMapping3(@PathVariable("userId") String userId,@PathVariable("topicId") String topicId,Map map){
        System.out.println(userId);
        System.out.println(topicId);

        map.put("v1",userId);
        map.put("v2",topicId);
        return  showJsp;
    }


    /** Ant风格的URL路径映射
     * http://127.0.0.1:8080/UrlMapping/ant/xxxx/1234.action
     * @return
     */
    @RequestMapping(value="/ant/**/{id}")
    public String antMapping(@PathVariable("id") String id,Map map){
        map.put("v1",id);
        return showJsp;
    }

    /**
     * 正则表达式风格的URL路径映射
     * http://127.0.0.1:8080/UrlMappingant/regular/12345-555.action
     * @return
     */
    @RequestMapping(value="/regular/{categoryCode:\\d+}-{pageNumber:\\d+}")
    public String regularExpressionMapping(@PathVariable("categoryCode")String categoryCode,@PathVariable("pageNumber")String pageNumber,Map map){
        map.put("v1",categoryCode);
        map.put("v2",pageNumber);
        return showJsp;
    }

    /** 请求方法限定
     *  限定get
     * @return
     */
    @RequestMapping(value="/method/get", method = {RequestMethod.GET})
    public String methodMapping1(Map map){
        map.put("v1","get");
        return showJsp;
    }

    /** 请求方法限定
     *  限定get
     * @return
     */
    @RequestMapping(value="/method/getAndPost", method = {RequestMethod.GET,RequestMethod.POST})
    public String methodMapping2(Map map){
        map.put("v1","get");
        map.put("v2","post");
        return showJsp;
    }

    /**
     *  参数限定 映射1
     *  http://127.0.0.1:8080/UrlMapping/parameter1.action?create
     * @param map
     * @return
     */
    @RequestMapping(value="/parameter1.action",params = "create")
    public String parameterMapping1(Map map){
        map.put("v1","parameter");
        return showJsp;
    }
    /**
     *  参数限定 映射2
     *  http://127.0.0.1:8080/UrlMapping/parameter.action?abc
     * @param map
     * @return
     */
    @RequestMapping(value="/parameter2.action",params = "!create")
    public String parameterMapping2(Map map){
        map.put("v1","no parameter");
        return showJsp;
    }
    /**
     *  参数限定 映射3
     *  http://127.0.0.1:8080/UrlMapping/parameter3.action?create=1
     * @param map
     * @return
     */
    @RequestMapping(value="/parameter3.action",params = "create=1")
    public String parameterMapping3(Map map){
        map.put("v1","parameter = 1 ");
        return showJsp;
    }
    /**
     *  参数限定 映射1
     *  http://127.0.0.1:8080/UrlMapping/parameter4.action?create=4
     * @param map
     * @return
     */
    @RequestMapping(value="/parameter4.action",params = "create!=1")
    public String parameterMapping4(Map map){
        map.put("v1","parameter != 1");
        return showJsp;
    }

    /**
     * @param date
     * @param map
     * @return
     */
    @RequestMapping(value="/converter.action")
    public String converterMapping(Date date,Map map){
        System.out.println(date);
        map.put("v1","converter");
        return showJsp;
    }
    @RequestMapping(value="/handleJson.action")
    @ResponseBody
    public List<User> handleJson(@RequestBody User[] users){
        for (int i = 0; i < users.length; i++) {
            System.out.println(users[i]);
            System.out.println("**********************");
        }
        List list=new ArrayList();
        User user1=new User();
        user1.setFirstName("ning");
        user1.setSecondName("shaolin");

        User user2=new User();
        user2.setFirstName("dai");
        user2.setSecondName("jingjia");
        list.add(user1);
        list.add(user2);
        return list;
    }
}
